<?php
namespace Admin\Controller;
use Think\Controller;
//分类管理
class TermsController extends CommonController {
    
    public function listc()
    {
        
         
  // 查询数据
        $data = $this -> get_cate();
        $data=json_encode($data);         
        $this->assign('data',$data);
          
        $this->display();
    }
   
   
     //字段列表
    public function fieldList()
    {
      //  echo 'wwww';
        $term_id = I('get.term_id');
        $data = M('field')->where('term_id='.$term_id)->select();
        $this->assign('data',$data);
        $this->assign('term_id',$term_id);
        $this->display('fieldlist');
    }
    //添加字段
    public function addField()
    {
        $term_id = I('get.term_id');
        $this->assign('term_id',$term_id);
        $this->display('addfield');
    }
    public function addFieldSub()
    {
        //  print_r(I('post.'));exit;
        //判断是否已添加过该字段
       
        $data = I('post.');
        //dump($data);
        //exit;
        $term_id = $data['term_id'];
        $re = M('field')->where("term_id= $term_id and field = '".$data['field']."'")->getField('id');
        if($re)
        {
             $this->error('表中已存在该字段');exit;
        }
        $field_model = D('Field');
        $re = $field_model->add($data);
        
        $re = $re = M('field')->add(I('post.'));
        if($re)
       {
         $this->success('添加成功');
       }else
       {
            $this->error('添加失败');
       }
    }
    //修改字段
    public function editField()
    {
        $term_id = I('get.term_id');
        $id = I('get.id');
        $data = M('field')->where('id='.$id)->find();
        $this->assign('id',$id);
        $this->assign('term_id',$term_id);
        $this->assign('data',$data);
        $this->display('editfield');
    }
    //修改字段   提交表单
    public function editFieldSub()
    {
        $data = I('post.');
        M('field')->where('id ='.$data['id'])->save($data);
        $field_model = D('Field');
        $re = $field_model->save($data);
        if($re!==false)
        {
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    public function get_cate($pid)
    {
        if (!$pid) {
            $pid = 0;
        }
        $data = M('Category') -> where(array('parent_id' => $pid)) -> select();

        foreach ($data as $k => $v) {
           $data[$k]['son'] = $this ->  get_cate($v['cat_id']);
        }
        return $data;
    }   

}